package com.couldsail.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.couldsail.takeaway.context.BaseContext;
import com.couldsail.takeaway.mapper.ShoppingCartMapper;
import com.couldsail.takeaway.pojo.dto.ShoppingCartDTO;
import com.couldsail.takeaway.pojo.entity.Products;
import com.couldsail.takeaway.pojo.entity.ShoppingCart;
import com.couldsail.takeaway.pojo.vo.ShoppingCartVO;
import com.couldsail.takeaway.service.ProductsService;
import com.couldsail.takeaway.service.ShoppingCartService;
import com.couldsail.takeaway.utils.UniqueIdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    private final ShoppingCartMapper shoppingCartMapper;
    private final ProductsService productsService;
    /**
     * 获取用户购物车
     * @param shopId
     * @return List<ShoppingCartVO
     */
    @Override
    public List<ShoppingCartVO> getShoppingCartList(Integer shopId) {
        String currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getShopId,shopId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        List<ShoppingCartVO> shoppingCartVOList = new ArrayList<>();

        for (ShoppingCart shoppingCart : shoppingCarts) {
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
            // 手动设置属性或使用BeanUtils.copyProperties进行简单属性复制
            BeanUtils.copyProperties(shoppingCart, shoppingCartVO);
            shoppingCartVO.setId(shoppingCart.getProductId());
            shoppingCartVOList.add(shoppingCartVO);
        }

        return shoppingCartVOList;
    }
    /**
     * 更新用户购物车
     * @param shoppingCartDTO
     */
    @Override
    public void updateShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        String currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getProductId,shoppingCartDTO.getProductId()).eq(ShoppingCart::getShopId,shoppingCartDTO.getShopId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        Products productId = productsService.getProductId(shoppingCartDTO.getProductId());

        if (!shoppingCarts.isEmpty()) {
            if (shoppingCartDTO.getAmount() <= 0) {
                log.info("当前正在删除购物车数据");
                shoppingCartMapper.delete(queryWrapper);
            }else {
                log.info("当前正在修改购物车数据{}",shoppingCartDTO.getAmount());
                LambdaUpdateWrapper<ShoppingCart> updateWrapper =  new LambdaUpdateWrapper<>();
                updateWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getProductId,shoppingCartDTO.getProductId()).eq(ShoppingCart::getShopId,shoppingCartDTO.getShopId())
                        .set(ShoppingCart::getAmount,shoppingCartDTO.getAmount())
                        .set(ShoppingCart::getImageUrl,productId.getImageUrl())
                        .set(ShoppingCart::getName,productId.getName())
                        .set(ShoppingCart::getNumber,shoppingCartDTO.getNumber())
                        .set(ShoppingCart::getProductSpecs,shoppingCartDTO.getProductSpecs())
                        .set(ShoppingCart::getDescription,productId.getDescription());
                shoppingCartMapper.update(updateWrapper);
            }
        }else {
            log.info("当前正在新增购物车数据");
            shoppingCartMapper.insert(ShoppingCart.builder()
                            .id(UniqueIdGenerator.generateUniqueId())
                            .name(productId.getName())
                            .imageUrl(productId.getImageUrl())
                            .productId(productId.getId())
                            .productSpecs(shoppingCartDTO.getProductSpecs())
                            .description(productId.getDescription())
                            .userId(BaseContext.getCurrentId())
                            .shopId(shoppingCartDTO.getShopId())
                            .number(shoppingCartDTO.getNumber())
                            .amount(shoppingCartDTO.getAmount())
                    .build());
        }

    }
    /**
     * 清空用户购物车
     */
    public void deleteAllShoppingCart(Integer shopId){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()).eq(ShoppingCart::getShopId,shopId);
        shoppingCartMapper.delete(queryWrapper);
    }
}
